﻿Imports System.IO
Public Class frmTrangChu
    Public tableDangTin As New DataTable
    Public tableKhuVuc As New DataTable
    Public tableLoaiNha As New DataTable
    Public chuoiMaHoa As String
    Public _iD As Integer
    Public Property ID() As Integer
        Get
            Return _iD
        End Get
        Set(ByVal value As Integer)
            _iD = value
        End Set
    End Property
    Private Function KiemTraCauHinh(ByVal path As String) As Boolean
        Dim objReader As StreamReader
        objReader = New StreamReader(path)
        chuoiMaHoa = objReader.ReadLine()
        objReader.Close()
        If chuoiMaHoa <> "" Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub frmChinh_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If KiemTraCauHinh("config.txt") = False Then
            Dim frmCon As New frm_ConnectDatabase
            frmCon.ShowDialog()
        End If
        Dim dtin As New DangTinBUS
        tableDangTin = dtin.GetAll()
        dgvChinh.DataSource = tableDangTin
        rbpTaiKhoan.Visible = False
        rbpDSTin.Visible = False
        rbpQLDangKiThue.Visible = False
        rbpCapNhatTin.Visible = False
        rbpXoaTin.Visible = False
        rbpXemChiTietTinNha.Visible = False
        barQuanTri.Enabled = False
        rbpQLDangTin.Visible = False
        rbpCongCu.Visible = False
        pnTimKiem.Hide()
        gThoat.Hide()
        '------------------------------------------------------------------------------------------------------------
        tableKhuVuc = kvBUS.GetAll()
        For i As Integer = 0 To tableKhuVuc.Rows.Count - 1
            cbTK_KhuVuc.Items.Add(tableKhuVuc.Rows(i)("TenKhuVuc").ToString())
        Next
        cbTK_KhuVuc.SelectedIndex = 0
        '------------------------------------------------------------------------------------------------------------
        tableLoaiNha = loainhaBUS.GetAll()
        For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
            cbTK_LoaiNha.Items.Add(tableLoaiNha.Rows(i)("TenLoaiNha").ToString())
        Next
        cbTK_LoaiNha.SelectedIndex = 0
        '------------------------------------------------------------------------------------------------------------
        cbTK_GiaTu.Items.Add("500000")
        cbTK_GiaTu.Items.Add("600000")
        cbTK_GiaTu.Items.Add("700000")
        cbTK_GiaTu.Items.Add("800000")
        cbTK_GiaTu.Items.Add("900000")
        cbTK_GiaTu.Items.Add("1000000")
        cbTK_GiaTu.SelectedIndex = 0
        '------------------------------------------------------
        cbTK_GiaDen.Items.Add("1000000")
        cbTK_GiaDen.Items.Add("1500000")
        cbTK_GiaDen.Items.Add("2000000")
        cbTK_GiaDen.Items.Add("4000000")
        cbTK_GiaDen.Items.Add("6000000")
        cbTK_GiaDen.Items.Add("8000000")
        cbTK_GiaDen.Items.Add("10000000")
        cbTK_GiaDen.SelectedIndex = 2 
    End Sub

    Private Sub btnMoTimKiem_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnMoTimKiem.ItemClick
        pnTimKiem.Show()
    End Sub
    Dim flag As Boolean = False
    Dim loaiTK As Integer = 0
    Private Sub ChucNangSinhVien()
        gTaiKhoan.Hide()
        gThoat.Show()
        rbpTaiKhoan.Visible = True
        rbpDSTin.Visible = True
        rbpQLDangKiThue.Visible = True
        rbpDangTin.Visible = False
        rbpXemChiTietTinNha.Visible = True
        rbpQLDangTin.Visible = True
        btnTrangQuanTri.Visible = False
        flag = True
        lbTenDangNhap.Text = txtTenDangNhap.Text
        pnTimKiem.Show()
    End Sub
    Private Sub ChucNangChuNha()
        gTaiKhoan.Hide()
        gThoat.Show()
        rbpTaiKhoan.Visible = True
        rbpDSTin.Visible = True
        rbpCapNhatTin.Visible = True
        rbpDangTin.Visible = True
        rbpXoaTin.Visible = True
        rbpXemChiTietTinNha.Visible = True
        rbpQLDangTin.Visible = True
        btnTrangQuanTri.Visible = False
        flag = True
        gThoat.Show()
        lbTenDangNhap.Text = txtTenDangNhap.Text
        pnTimKiem.Show()
    End Sub
    Private Sub ChucNangQuanTri()
        gTaiKhoan.Hide()
        barQuanTri.Enabled = True
        rbpTaiKhoan.Visible = True
        rbpDSTin.Visible = True
        rbpQLDangKiThue.Visible = False
        rbpCapNhatTin.Visible = False
        rbpXoaTin.Visible = False
        rbpDangTin.Visible = False
        btnTrangQuanTri.Visible = True
        flag = True
        gThoat.Show()
        lbTenDangNhap.Text = txtTenDangNhap.Text       
    End Sub
    Public tkDTO As New TaiKhoanDTO
    Public tkBUS As New TaiKhoanBUS
    Public svDTO As New SinhVienDTO
    Public svBUS As New SinhVienBUS
    Public cnDTO As New ChuNhaDTO
    Public cnBUS As New ChuNhaBUS
    Public kvDTO As New KhuVucDTO
    Public kvBUS As New KhuVucBUS
    Public loainhaDTO As New LoaiNhaDTO
    Public loainhaBUS As New LoaiNhaBUS
    Public dangtinDTO As New DangTinDTO
    Public dangtinBUS As New DangTinBUS

    Private Sub btnDangNhap_Click(sender As System.Object, e As System.EventArgs) Handles btnDangNhap.Click

        If txtMatKhau.Text <> "" And txtTenDangNhap.Text <> "" Then

            tkDTO.TenTaiKhoan = txtTenDangNhap.Text
            tkDTO.MatKhau = txtMatKhau.Text
            loaiTK = tkBUS.CheckDangNhap(tkDTO)
            '------------------------------------------------------------------------------------------------------------
            If loaiTK = 1 Then
                ChucNangQuanTri()
            ElseIf loaiTK = 2 Then
                ChucNangSinhVien()
                svBUS.GetItemID(txtTenDangNhap.Text, svDTO, tkDTO)
            ElseIf loaiTK = 3 Then
                cnBUS.GetSVTK(txtTenDangNhap.Text, cnDTO, tkDTO)
                ChucNangChuNha()
            Else
                txtMatKhau.Text = ""
            End If

        Else
            MessageBox.Show("Xin nhập thông tin đăng nhập", "Thông báo")
        End If
    End Sub

    Private Sub btnDangXuat_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDangXuat.ItemClick
        rbpTaiKhoan.Visible = False
        gTaiKhoan.Show()
        txtMatKhau.Text = ""
        txtTenDangNhap.Text = ""
        flag = False
    End Sub

    Private Sub btnDangTin_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDangTin.ItemClick
        If flag = False Then
            MessageBox.Show("Bạn phải đăng nhập tài khoản", "Thông báo")
        ElseIf flag = True Then
            Dim frmDangTin As New frmThemMoiTinBai
            frmDangTin.Taikhoan = txtTenDangNhap.Text
            frmDangTin.ShowDialog()
        End If
    End Sub

    Private Sub btnDangKy_Click(sender As System.Object, e As System.EventArgs) Handles btnDangKy.Click
        Dim frmDangk As New frmDangKiTaiKhoan
        frmDangk.ShowDialog()
        'Me.Hide()
    End Sub
    Public tableEmpty As New DataTable
    Private Sub btnDanhSachTin_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDanhSachTin.ItemClick
        'If flag = False Then
        '    MessageBox.Show("Bạn phải đăng nhập tài khoản", "Thông báo")
        'Else
        Dim dtin As New DangTinBUS
        tableDangTin = dtin.GetAll()
        dgvChinh.DataSource = tableDangTin
        dgvChinh.Show()
        dgvDangKy.Hide()
        'For i As Integer = 0 To tableDangTin.Rows.Count - 1
        '    Dim nhaDTO As New DangTinDTO
        '    nhaDTO.SucChua = Convert.ToInt32(tableDangTin.Rows(i)("SucChua").ToString())
        'Next
        'End If
    End Sub

    Private Sub btnThayDoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThayDoi.ItemClick
        Dim frmDoiThongTin As New frmCapNhatThongTin
        If loaiTK = 2 Then
            frmDoiThongTin._svDTO = svDTO
            frmDoiThongTin._tkDTO = tkDTO
        ElseIf loaiTK = 3 Then
            frmDoiThongTin._cnDTO = cnDTO
            frmDoiThongTin._tkDTO = tkDTO
        End If
        frmDoiThongTin.ShowDialog()
    End Sub

    Private Sub btnDoiMK_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDoiMK.ItemClick
        Dim frmDoiMK As New frmDoiMatKhau
        frmDoiMK._tkDTO = tkDTO
        frmDoiMK.ShowDialog()
    End Sub

    Private Sub btnThemDKThue_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemDKThue.ItemClick
        Dim frmdk As New frmThemDangKyThue
        svBUS.GetItemID(txtTenDangNhap.Text, svDTO, tkDTO)
        frmdk.svDTO = svDTO
        frmdk.taikhoanDTO = tkDTO
        frmdk.ShowDialog()
    End Sub
    Private Sub btnCapNhatDKThue_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCapNhatDKThue.ItemClick
        If dkDTO.TieuDe <> "" Then
            Dim frmcapnhat As New frmCapNhatDangKy
            frmcapnhat._dkDTO = dkDTO
            frmcapnhat._svDTO = svDTO
            frmcapnhat.ShowDialog()
        End If
    End Sub

    Private Sub bntCapNhatTin_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCapNhatTin.ItemClick
        If _dangTinDTO.TieuDe <> "" Then
            Dim frmCapNhatTin As New frmCapNhatTinBai
            frmCapNhatTin._dangtinDTO = _dangTinDTO
            frmCapNhatTin.ShowDialog()
        End If
    End Sub

    Private Sub btnQuanTri_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnQuanTri.ItemClick
        Dim frmQT As New frmQuanTri
        frmQT.Show()
        Me.Hide()
    End Sub

    Private Sub btnThoatDN_Click(sender As System.Object, e As System.EventArgs) Handles btnThoatDN.Click
        dgvDangKy.DataSource = tableEmpty
        dgvChinh.DataSource = tableEmpty
        rbpTaiKhoan.Visible = False
        rbpDSTin.Visible = False
        rbpQLDangKiThue.Visible = False
        rbpCapNhatTin.Visible = False
        rbpXoaTin.Visible = False
        rbpXemChiTietTinNha.Visible = False
        rbpQLDangTin.Visible = False
        rbpCongCu.Visible = False
        gThoat.Hide()
        gTaiKhoan.Show()
        txtMatKhau.Text = ""
        flag = False
    End Sub

    Private Sub barQuanTri_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barQuanTri.ItemClick
        Dim frmQT As New frmQuanTri
        frmQT.Show()
        Me.Hide()
    End Sub
    Public tableDangKyID As New DataTable
    Public dkDTO As New DangKyDTO
    Public dkBUS As New DangKyBUS
    Dim _dangTinDTO As New DangTinDTO
    Dim _diaChi As New DiaChiDTO
    Private Sub dgvChinh_RowEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvChinh.RowEnter
        Try
            _dangTinDTO.MaDangTin = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaDangTin").Value.ToString())
            _diaChi.MaDiaChi = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaDiaChi").Value.ToString())
            '------------------------------------------------------------------------------------------------------
            _dangTinDTO.ChoNamThue = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("ChoNamThue").Value.ToString())
            _dangTinDTO.ChoNuThue = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("ChoNuThue").Value.ToString())
            _dangTinDTO.ChoNauAn = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("ChoNauAn").Value.ToString())
            _dangTinDTO.GanCho = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("GanCho").Value.ToString())
            _dangTinDTO.GanTramXeBuyt = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("GanTramXeBuyt").Value.ToString())
            _dangTinDTO.TruyenHinhCap = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("TruyenHinhCap").Value.ToString())
            _dangTinDTO.Internet = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("Internet").Value.ToString())
            _dangTinDTO.DienTich = Convert.ToSingle(dgvChinh.Rows(e.RowIndex).Cells("DienTich").Value.ToString())
            _dangTinDTO.MaDiaChi = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaDiaChi").Value.ToString())
            _dangTinDTO.MaLoaiNha = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaLoaiNha").Value.ToString())
            _dangTinDTO.MaLoaiTin = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaLoaiTin").Value.ToString())
            _dangTinDTO.OGhep = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("OGhep").Value.ToString())
            _dangTinDTO.OChungChu = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("OChungChu").Value.ToString())
            _dangTinDTO.SucChua = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("SucChua").Value.ToString())
            _dangTinDTO.TieuDe = dgvChinh.Rows(e.RowIndex).Cells("TieuDeTin").Value.ToString()
            _dangTinDTO.GiaThue = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("GiaThue").Value.ToString())
            _dangTinDTO.MaChuNha = Convert.ToInt32(dgvChinh.Rows(e.RowIndex).Cells("MaChuNha").Value.ToString())
            _dangTinDTO.ThoiGianDangTin = dgvChinh.Rows(e.RowIndex).Cells("ThoiGianDangTin").Value.ToString()
            _dangTinDTO.ThoiGianDongTin = dgvChinh.Rows(e.RowIndex).Cells("ThoiGianDongTin").Value.ToString()
            cnDTO = cnBUS.GetItemID(_dangTinDTO.MaChuNha)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnXoaTin_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoaTin.ItemClick
        If _dangTinDTO.TieuDe <> "" Then
            Dim result As DialogResult = MessageBox.Show("Bạn có chắc chắn xóa không?", "Câu hỏi", MessageBoxButtons.YesNo)
            If result = DialogResult.Yes Then
                Dim dangtinBUS As New DangTinBUS
                Dim diachiBUS As New DiaChiBUS
                diachiBUS.Delete(_diaChi)
                dangtinBUS.Delete(_dangTinDTO)
                '---------------------------------------------------------------------------------------------------------------------------
                Dim dtin As New DangTinBUS
                tableDangTin = dtin.GetAll()
                dgvChinh.DataSource = tableDangTin
                dgvChinh.Refresh()
            End If
        End If

    End Sub
    Dim tableTimKiem As DataTable

    Private Sub btnTimKiem_Click(sender As System.Object, e As System.EventArgs) Handles btnTimKiem.Click
        tableTimKiem = dangtinBUS.SeachTin(cbTK_LoaiNha.Text, cbTK_KhuVuc.Text, cbTK_GiaTu.Text, cbTK_GiaDen.Text)
        dgvChinh.DataSource = tableTimKiem
        dgvChinh.Show()
        dgvDangKy.Hide()
    End Sub

    Private Sub dgvDangKy_RowEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDangKy.RowEnter
        Try
            dkDTO.MaDangKy = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("MaDangKy").Value.ToString())
            '------------------------------------------------------------------------------------------------------
            dkDTO.MaKhuVucThue = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("MaKhuVucThue").Value.ToString())
            dkDTO.ChoNamThue = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("ChoNamThue").Value.ToString())
            dkDTO.ChoNuThue = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("ChoNuThue").Value.ToString())
            dkDTO.ChoNauAn = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("ChoNauAn").Value.ToString())
            dkDTO.GanCho = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("GanCho").Value.ToString())
            dkDTO.GanTramXeBuyt = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("GanTramXeBuyt").Value.ToString())
            dkDTO.TruyenHinhCap = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("TruyenHinhCap").Value.ToString())
            dkDTO.Internet = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("Internet").Value.ToString())
            dkDTO.DienTich = Convert.ToSingle(dgvDangKy.Rows(e.RowIndex).Cells("DienTich").Value.ToString())
            dkDTO.MaLoaiNha = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("MaLoaiNha").Value.ToString())
            dkDTO.OGhep = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("OGhep").Value.ToString())
            dkDTO.OChungChu = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("OChungChu").Value.ToString())
            dkDTO.SucChua = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("SucChua").Value.ToString())
            dkDTO.TieuDe = dgvDangKy.Rows(e.RowIndex).Cells("TieuDe").Value.ToString()
            dkDTO.GiaThue = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("GiaThue").Value.ToString())
            dkDTO.MaSinhVien = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("MaSinhVien").Value.ToString())
            dkDTO.TrangThai = Convert.ToInt32(dgvDangKy.Rows(e.RowIndex).Cells("TrangThai").Value.ToString())
            dkDTO.ThoiGianDangKy = Now
            dkDTO.ThoiGianDong = Now
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnXoaDKThue_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoaDKThue.ItemClick
        If dkDTO.TieuDe <> "" Then
            Dim result1 As DialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa nó?", "Câu hỏi", MessageBoxButtons.YesNo)
            If result1 = DialogResult.Yes Then
                dkBUS.Delete(dkDTO)
                tableDangKyID = dkBUS.GetItemID(dkDTO.MaSinhVien)
                dgvDangKy.DataSource = tableDangKyID
            End If
        End If
    End Sub

    Private Sub btnDSDangKyThue_2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDSDangKyThue_2.ItemClick
        tableDangKyID = dkBUS.GetItemID(svDTO.MaSinhVien)
        dgvDangKy.DataSource = tableDangKyID
        dgvDangKy.Show()
        dgvChinh.Hide()
    End Sub

    Private Sub btnXemChiTietTin_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXemChiTietTin.ItemClick
        If _dangTinDTO.TieuDe <> "" Then
            Dim frmXemChiTiet As New frmXemChiTietTinNha
            frmXemChiTiet._dangtinDTO = _dangTinDTO
            frmXemChiTiet._chunhaDTO = cnDTO
            frmXemChiTiet.ShowDialog()
        End If
    End Sub

    Private Sub btnXemChiTietDangKy_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXemChiTietDangKy.ItemClick
        If dkDTO.TieuDe <> "" Then
            Dim frmXemChiTiet As New frmXemChiTietDK
            frmXemChiTiet._dkDTO = dkDTO
            frmXemChiTiet._svDTO = svDTO
            frmXemChiTiet.ShowDialog()
        End If
    End Sub

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub SimpleButton1_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton1.Click
        Me.Close()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Dim frm As New frmVePhanMem
        frm.ShowDialog()
    End Sub

    Private Sub btnTrangQuanTri_Click(sender As System.Object, e As System.EventArgs) Handles btnTrangQuanTri.Click
        Dim frmQT As New frmQuanTri
        frmQT.Show()
        Me.Hide()
    End Sub
    Private Sub txtMatKhau_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMatKhau.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtMatKhau.Text <> "" And txtTenDangNhap.Text <> "" Then

                tkDTO.TenTaiKhoan = txtTenDangNhap.Text
                tkDTO.MatKhau = txtMatKhau.Text
                loaiTK = tkBUS.CheckDangNhap(tkDTO)
                '------------------------------------------------------------------------------------------------------------
                If loaiTK = 1 Then
                    ChucNangQuanTri()
                ElseIf loaiTK = 2 Then
                    ChucNangSinhVien()
                    svBUS.GetItemID(txtTenDangNhap.Text, svDTO, tkDTO)
                ElseIf loaiTK = 3 Then
                    cnBUS.GetSVTK(txtTenDangNhap.Text, cnDTO, tkDTO)
                    ChucNangChuNha()
                Else
                    txtMatKhau.Text = ""
                End If

            Else
                MessageBox.Show("Xin nhập thông tin đăng nhập", "Thông báo")
            End If
        End If
    End Sub
End Class